package com.xbongbong.paas.help.factory.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.abstracts.AbstractOperatorFieldHandler;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.help.FormDataFormExplainHelper;
import com.xbongbong.paas.help.factory.OperatorFieldHandler;
import com.xbongbong.paas.pojo.OperationsPojo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.pro.businessrule.pojo.BusinessRuleInfoPojo;
import com.xbongbong.saas.enums.UserTeamEnum;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @version v1.0
 * @date 2020/3/4 21:11
 * @since v1.0
 */
@Component
public class OperatorUserGroupHandlerImpl extends AbstractOperatorFieldHandler {

    @Resource
    private FormDataFormExplainHelper formDataFormExplainHelper;


    @Override
    public List<Integer> fieldType() {
        return Arrays.asList(FieldTypeEnum.USER_GROUP.getType());
    }


    @Override
    public Object handlerTargetFormField(PaasFormDataEntity paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField,
                                         BusinessRuleInfoPojo businessRuleInfoPojo, OperationsPojo operation) throws XbbException {
        Map<Long, List<String>> ownerIdMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Map<Long, List<String>> coUserIdMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Integer businessType = businessRuleInfoPojo.getBusinessType();
        if (Objects.equals(FieldTypeEnum.OWNERID.getType(), fieldAttrEntity.getFieldType())) {
            if (UserTeamEnum.hasTeam(businessType)) {
                List<Long> idIn = new ArrayList<>();
                idIn.add(paasFormDataEntity.getId());
                formDataFormExplainHelper.getUserIdMap(idIn, paasFormDataEntity.getCorpid(), businessType, false, ownerIdMap, coUserIdMap);

                List<String> ownerId = ownerIdMap.get(paasFormDataEntity.getId());
                if (Objects.isNull(ownerId) || ownerId.isEmpty()) {
                    return null;
                }
                return ownerId;
            } else {
                return Collections.singletonList(paasFormDataEntity.getOwnerId());
            }
        } else {
            return data.get(fieldAttrEntity.getAttr());
        }
    }

    @Override
    public Object handlerFixedValue(FieldAttrEntity targetField, BusinessRuleInfoPojo businessRuleInfoPojo, OperationsPojo operation) throws XbbException {
        JSONArray userList = JSON.parseArray(JSON.toJSONString(operation.getValue()));
        if (CollectionsUtil.isNotEmpty(userList)) {
            List<Object> idList = new ArrayList<>();
            for (int i = 0; i < userList.size(); i++) {
                idList.add(userList.getJSONObject(i).get("id"));
            }
           return idList;
        }
        return null;
    }
}
